﻿
using EasyTest.Application;
using EasyTest.Application.Const;
using NewLife.Reflection;
namespace EasyTest.Application;

/// <summary>
/// 题库表服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class EtQuestionBankService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<EtQuestionBank> _etQuestionBankRep;
    public EtQuestionBankService(SqlSugarRepository<EtQuestionBank> etQuestionBankRep)
    {
        _etQuestionBankRep = etQuestionBankRep;
    }

    /// <summary>
    /// 分页查询题库表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询题库表")]
    public async Task<SqlSugarPagedList<EtQuestionBankOutput>> Page(PageEtQuestionBankInput input)
    {
        var query = _etQuestionBankRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>u.Name.Contains(input.SearchKey))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name?.Trim()), u => u.Name.Contains(input.Name))
            .WhereIF(input.Status.HasValue, u => u.Status == (StatusEnum)input.Status.Value)
            .Select<EtQuestionBankOutput>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加题库表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加题库表")]
    public async Task<long> Add(AddEtQuestionBankInput input)
    {
        var entity = input.Adapt<EtQuestionBank>();
        await _etQuestionBankRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除题库表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除题库表")]
    public async Task Delete(DeleteEtQuestionBankInput input)
    {
        var entity = await _etQuestionBankRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _etQuestionBankRep.FakeDeleteAsync(entity);   //假删除
        //await _etQuestionBankRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新题库表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新题库表")]
    public async Task Update(UpdateEtQuestionBankInput input)
    {
        var entity = input.Adapt<EtQuestionBank>();
        await _etQuestionBankRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取题库表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取题库表")]
    public async Task<EtQuestionBank> Detail([FromQuery] QueryByIdEtQuestionBankInput input)
    {
        return await _etQuestionBankRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取题库表列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取题库表列表")]
    public async Task<List<EtQuestionBankOutput>> List([FromQuery] PageEtQuestionBankInput input)
    {
        return await _etQuestionBankRep.AsQueryable().Select<EtQuestionBankOutput>().ToListAsync();
    }





}
